package gui;

import javax.swing.JPanel;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import negocios.Cor;
import negocios.Fachada;
import javax.swing.WindowConstants;
import javax.swing.text.Document;
import javax.swing.text.MaskFormatter;

public class CadastroDeCor extends JDialog {
	
	private final int limiteCaracteres = 50;
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel lTitulo = null;
	private JLabel lCodigo = null;
	private JFormattedTextField tCodigo = null;
	private JLabel lDescricao = null;
	private JTextField tDescricao = null;
	private JButton bConfirmar = null;
	private JButton bLimpar = null;
	private JButton bCancelar = null;
	private MaskFormatter mCod = null;  //  @jve:decl-index=0:
	
	private MaskFormatter getMCod(){
		if(mCod==null){
			try {
				mCod = new MaskFormatter("####");
				mCod.setPlaceholderCharacter('0');
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		return mCod;
	}

	/**
	 * @param owner
	 */
	public CadastroDeCor(Frame owner) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(475, 225);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lDescricao = new JLabel();
			lDescricao.setBounds(new Rectangle(87, 111, 64, 16));
			lDescricao.setText("Descricao: ");
			lCodigo = new JLabel();
			lCodigo.setBounds(new Rectangle(87, 82, 68, 16));
			lCodigo.setText("Codigo: ");
			lTitulo = new JLabel();
			lTitulo.setBounds(new Rectangle(115, 20, 288, 23));
			lTitulo.setText("Cadastro de Cor");
			lTitulo.setFont(new Font("Tahoma", Font.BOLD, 25));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(lTitulo, null);
			jContentPane.add(lCodigo, null);
			jContentPane.add(getTCodigo(), null);
			jContentPane.add(lDescricao, null);
			jContentPane.add(getTDescricao(), null);
			jContentPane.add(getBConfirmar(), null);
			jContentPane.add(getBLimpar(), null);
			jContentPane.add(getBCancelar(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes tCodigo	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JFormattedTextField getTCodigo() {
		if (tCodigo == null) {
			tCodigo = new JFormattedTextField(getMCod());
			tCodigo.setBounds(new Rectangle(168, 76, 67, 26));
			tCodigo.setHorizontalAlignment(JTextField.CENTER);
		}
		return tCodigo;
	}

	/**
	 * This method initializes tDescricao	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTDescricao() {
		if (tDescricao == null) {
			tDescricao = new JTextField();
			tDescricao.addKeyListener(new java.awt.event.KeyAdapter() {  
				    public void keyTyped(java.awt.event.KeyEvent e) {  
				                    int k = e.getKeyChar();  
				       if (tDescricao.getText().length() < limiteCaracteres) {  
				                         //deixe passar  
				       } else {  
				          e.setKeyChar((char) KeyEvent.VK_CLEAR);  
				       }  
				   }  
				    
				   });  
			tDescricao.setBounds(new Rectangle(168, 105, 173, 31));
		}
		return tDescricao;
	}

	/**
	 * This method initializes bConfirmar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBConfirmar() {
		if (bConfirmar == null) {
			bConfirmar = new JButton();
			bConfirmar.setBounds(new Rectangle(75, 161, 93, 20));
			bConfirmar.setText("Confirmar");
			bConfirmar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String cod = tCodigo.getText();
					String desc = tDescricao.getText();
					Cor cor = new Cor();
					cor.setCodigo(cod);					
					cor.setDescricao(desc.toUpperCase());
					try {
						if ((tCodigo.getText().equals("")) || (tDescricao.getText().equals(""))){
							JOptionPane.showMessageDialog(null,"Por favor, preencha todos os campos!","Sig Confex - Erro!",	JOptionPane.INFORMATION_MESSAGE,null);
							tCodigo.requestFocus();							
						}else{
							
							Cor x=null;
							//Teste de inser��o dupicada
							
							x = Fachada.getInstancia().ConsultarCor(cor,true);
							if (x == null){
							
								Fachada.getInstancia().IncluiCor(cor);
								JOptionPane.showMessageDialog(null,"Cor inserida com sucesso.","Confirmacao de inclusao",JOptionPane.INFORMATION_MESSAGE,null);
						
							}else JOptionPane.showMessageDialog(null,"Cor ja existente.","Confirmacao de inclusao",JOptionPane.ERROR_MESSAGE,null);
							   
							
						}
			} catch (Exception e1) {
				JOptionPane.showMessageDialog(null,"Cliente nao inserido.","Confirmacao de inclusao",JOptionPane.ERROR_MESSAGE,null);
				e1.printStackTrace();
			}
					tCodigo.setText("");
					tDescricao.setText("");
					tCodigo.requestFocus();
		}
	});
		}
		return bConfirmar;
	}

	/**
	 * This method initializes bLimpar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBLimpar() {
		if (bLimpar == null) {
			bLimpar = new JButton();
			bLimpar.setBounds(new Rectangle(176, 161, 93, 20));
			bLimpar.setText("Limpar");
			bLimpar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					tCodigo.setText("");
					tDescricao.setText("");
					tCodigo.requestFocus();
				}
		});
		}
		return bLimpar;
	}

	/**
	 * This method initializes bCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBCancelar() {
		if (bCancelar == null) {
			bCancelar = new JButton();
			bCancelar.setBounds(new Rectangle(278, 162, 93, 20));
			bCancelar.setText("Cancelar");
			bCancelar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					setVisible(false);
					dispose();
				}
			});
		}
		return bCancelar;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
